package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.common.OtwConstant;
import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.model.enums.MnSAPStatus;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnsapif041v2.dto.Purchase041DetailRequest;
import com.epoch.app.otsapplicationadaptersdk.mnsapif041v2.service.MnSapIf041V2Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnSapIf041Request;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommSDO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * SAP采购订单明细回传中台 sap->中台
 *
 * @author baizhuo
 * @version 1.0
 * @date 2024/1/10 11:01
 */
@Component
@Primary
public class MnSapIf041V2ServiceImpl implements MnSapIf041V2Service {

    private final Log log = Log.getLogger(MnSapIf041V2ServiceImpl.class);

    @Autowired
    private HttpUtils httpUtils;

    @Override
    @ProcessInvoker(remark = "MnSapIf041V2Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnSapCommResponse purchaseOrderDetailFeedback(MnSapIf041Request mnSapIf041Request) {
        log.info("MnSapIf041V2Service purchaseOrderDetailFeedback param:{}", JSON.toJSONString(mnSapIf041Request));
        //拿到041采购单的结果，给bc-link010接口
        MnSapCommResponse mnSapCommResponse = new MnSapCommResponse();
        MnSapCommSDO mnSapCommSDO = new MnSapCommSDO();
        try {
            Map<String, String> header = HttpTokenUtil.generateHttpHeader();
            String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
            String url = urlPrefix + "api/mn_bc_link/purchaseOrder041";
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(mnSapCommResponse));
            jsonObject.put("key", mnSapIf041Request.getITEM1().get(0).getVBELN());
            log.info("MnSapIf041V2ServiceImpl purchaseOrder041 call bc link interface,url:{}, header:{}, " + "param:{}",
                url, header, jsonObject.toJSONString());
            ResponseEntity<Result> post = httpUtils.post(url, header, jsonObject, Result.class);
            Result result = post.getBody();
            log.info("MnSapIf041V2ServiceImpl purchaseOrder041 result:{}", JSON.toJSONString(result));
            if (result.isSuccess()) {
                mnSapCommSDO.setStatus(MnSAPStatus.SUCCESS.getCode());
            } else {
                mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
                mnSapCommSDO.setErrCode(result.getErrorCode());
                mnSapCommSDO.setMessage(result.getMessage());
            }
        } catch (Exception e) {
            log.error("MnSapIf041V2Service purchaseOrder041 error", e);
            mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
            mnSapCommSDO.setErrCode(OtwConstant.ORDER_001_01_01_001);
            mnSapCommSDO.setMessage(MnSAPStatus.ERROR.getMsg());
        }
        mnSapCommResponse.setData(Lists.newArrayList(mnSapCommSDO));
        return mnSapCommResponse;
    }

    /**
     * purchase041Detail
     *
     * @param purchase041DetailRequest
     */
    @Override
    public MnSapCommResponse purchase041Detail(Purchase041DetailRequest purchase041DetailRequest) {
        log.info("MnSapIf041V2Service purchase041Detail param:{}", JSON.toJSONString(purchase041DetailRequest));
        //拿到041采购单的结果，给bc-link010接口
        MnSapCommResponse mnSapCommResponse = new MnSapCommResponse();
        MnSapCommSDO mnSapCommSDO = new MnSapCommSDO();
        try {
            Map<String, String> header = HttpTokenUtil.generateHttpHeader();
            String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
            String url = urlPrefix + "api/mn_bc_link/purchaseOrder041";
            JSONObject jsonObject = JSON.parseObject(purchase041DetailRequest.getMessage());
            if (!jsonObject.containsKey("ITEM1")) {
                mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
                mnSapCommSDO.setErrCode(OtwConstant.ORDER_001_01_01_001);
                mnSapCommSDO.setMessage("明细1为空");
            } else if (!jsonObject.containsKey("ITEM2")) {
                mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
                mnSapCommSDO.setErrCode(OtwConstant.ORDER_001_01_01_001);
                mnSapCommSDO.setMessage("明细2为空");
            } else {
                jsonObject.put("key", jsonObject.getJSONObject("HEADER").getString("MESSAGEID"));
                log.info(
                    "MnSapIf041V2ServiceImpl purchaseOrder041 call bc link interface,url:{}, header:{}, " + "param:{}",
                    url, header, jsonObject.toJSONString());
                ResponseEntity<Result> post = httpUtils.post(url, header, jsonObject, Result.class);
                Result result = post.getBody();
                log.info("MnSapIf041V2ServiceImpl purchaseOrder041 result:{}", JSON.toJSONString(result));
                if (result.isSuccess()) {
                    mnSapCommSDO.setStatus(MnSAPStatus.SUCCESS.getCode());
                } else {
                    mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
                    mnSapCommSDO.setErrCode(result.getErrorCode());
                    mnSapCommSDO.setMessage(result.getMessage());
                }
            }
        } catch (Exception e) {
            log.error("MnSapIf041V2Service purchaseOrder041 error", e);
            mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
            mnSapCommSDO.setErrCode(OtwConstant.ORDER_001_01_01_001);
            mnSapCommSDO.setMessage(MnSAPStatus.ERROR.getMsg());
        }
        mnSapCommResponse.setData(Lists.newArrayList(mnSapCommSDO));
        return mnSapCommResponse;
    }
}
